WiimoteLib

<-- Prev --------- Contents --------- Next -->

The WiimoteLib is similar to the VexLib, except it handles communication with the Wiimote, not the Vex's Bluetooth. The WiimoteLib was written by Brian Peek and has a variety of features that are available. Only a few will be used by our project. The code examples included assumes that wii_mote has been declared as a Wiimote in your main code (more on this later). As before, the Wiimote variable (named wii_mote) contains the functions we need, so we can access them with a "." followed by the function name we want. For example, "wii_mote.Connect()" is the first example we will talk about.

Connecting

In order to use the Wiimote in your program you will need to have something like this in order to use the pieces.
wii_mote = new Wiimote()
wii_mote.Connect();
wii_mote.SetReportType(InputReport.IRAccel, true);
The first line has to do with initializing the wii_mote variable; we'll talk more about this when we talk about classes and objects. The next line, "wii_mote.Connect();" tells the program to connect to the Wiimote. The last line simply tells the Wiimote what kind of information we will be asking it for later on. By default, the Wiimote will always tells us which of it's buttons are being pressed whenever we ask it for an update. The last line of code above basically says "When we ask for an update later, we also want to know about any Infrared (IR) lights you see, and what your accelerometers say". It might make a bit more sense later when we start using this information.

These three lines of code must be called before you get any information from the Wiimote.

ButtonState


Suppose we want to ask the wiimote what buttons are being pressed. The WiimoteLib makes this easy. First, you will need to know how to access the state of the wiimote. Remember we said that the variable wii_mote is declared as our Wiimote? Well to access its state, we will use "wii_mote.WiimoteState". To further organize things, we will have to explicitly get the state of the buttons on the wiimote (as opposed to the IR camera, accelerometers, etc) - and to access that, we can use "wii_mote.WiimoteState.ButtonState". Once we have that, we can just add the name of the button we are interested in, and the wiimote will tell us whether it is pressed or not!

The buttons include:
A, B, Home, Plus, Minus, One, Two, Up, Down, Left, and Right.

For example, suppose we wanted to know if the A button were being pressed. The following code asks the wiimote, and stores true or false in the boolean variable buttonpressed.
bool buttonpressed = wii_mote.WiimoteState.ButtonState.A
Now we can use it however we want in our code!

AccelStatepry-wiimote.gif


AccelState allows us to request the value of the accelerometer on the WiiMote. Accelerometers can detect how much the WiiMote is tilted in any direction. The WiimoteLib returns the accelerometer value in a specific range of values, so we are providing some code to normalize it between 0 and 1. This just means that every possible value of the accelerometer is being mapped into a value between 0 and 1 to make it easier to deal with.

In the code example below, turn_percent will be the value of the accelerometers, normalized between 0 and 1. You can then use that number to vary your motor speeds, etc.
float turn_percent = (wii_mote.WiimoteState.AccelState.Values.Y + 1.0f) / 2.0f;
See the "wii_mote.WiimoteState.AccelState.Values.Y" ? That should look similar to the code we used to get the button value - because that's exactly what it does, but for the accelerometer. It gets the value of the accelerometers in the Y axis (see the illustration). The rest of the stuff in that code does the math to normalize it between 0 and 1.

IRState


Similar to ButtonState and AccelState, the IRState gets the state of the Infrared camera on the wiimote.

The IRState has many functions associated with it. We will be using the RawPosition.X and RawPosition.Y to find the location of each of the infrared LEDs in space. This will come into play more in our later project, the Mapper robot. For now, know that it is very similar to the ButtonState and AccelState.

Now, let's give it all a try!

<-- Prev --------- Contents --------- Next -->